package com.testdemo.web.controller;

import java.util.List; 
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; 
import com.testdemo.web.question.IQuestion;
import com.testdemo.web.question.IShapeCalcQusition;
import com.testdemo.web.question.IShapeJudgeQusition;
import com.testdemo.web.service.QuestionService;
import com.testdemo.web.service.QusetionTypeService;
import com.testdemo.web.shapecalc.impl.QuestionFactory;
import com.testdemo.web.vo.Question;
import com.testdemo.web.vo.QuestionType;
import com.testdemo.web.vo.User;
 
@Controller
@RequestMapping("/doquestion")
public class DoQuestionController  {
	
	public class ErrCode
	{
		int code;
		String msg;
		String rightAnswer; 
		public int getCode() {
			return code;
		}
		public void setCode(int code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public String getRightAnswer() {
			return rightAnswer;
		}
		public void setRightAnswer(String rightAnswer) {
			this.rightAnswer = rightAnswer;
		}
		
		
	}
	
	@Resource
	private QuestionService questionService;
	
	@Resource
	private QusetionTypeService typeService;
	
	@RequestMapping(value = "/type", 
			method = RequestMethod.GET)
	public ModelAndView getType() {  
	 	List<QuestionType> list = typeService.listAll(null);
		ModelAndView mav = new ModelAndView("questionType");  
		mav.addObject("list", list);
		return mav; 
	}
	
	
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public ModelAndView getQuestion(
			@RequestParam("typeid") Integer typeid, 
			HttpSession session) { 
		User u = (User)session.getAttribute("currUser"); 
		if (u == null)
		{
			ModelAndView mav = new ModelAndView("loginUser");
			return mav;
		} 
		
		if (typeid == null)
		{
			typeid = (Integer)session.getAttribute("questionTypeId");  
		}  
		
		QuestionFactory qf = new QuestionFactory();		
		IQuestion question = qf.CreateQuestion(typeid);  
		ModelAndView mav;
		if (question.getPageType().equals("calc")){
			mav = new ModelAndView("calcQuestion");	
		}else
		{
			mav = new ModelAndView("judgeQuestion");  
		}
		session.setAttribute("currQuestion", question);  
		session.setAttribute("questionTypeId", typeid); 
		mav.addObject("username", u.getUserName());
		mav.addObject("question", question.getQusitionString());
		return mav; 
	}
	 
		   
	@RequestMapping(value = "/check", 
			method = RequestMethod.POST,
			consumes = MediaType.APPLICATION_JSON_VALUE, 
			produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ErrCode checkQuestion(
			@RequestBody JspAnswer answer,
		 HttpSession session) { 
		ErrCode code = new ErrCode();
		User u = (User)session.getAttribute("currUser"); 
		if (u == null)
		{ 
			code.setCode(0);
			code.setMsg("未登录，请登录！");
			return code;
			
		} 
		IQuestion question = (IQuestion)session.getAttribute("currQuestion"); 
		if ( question == null)
		{
			code.setCode(0);
			code.setMsg("未生成题面，请进入做题环节！");  
			return code;
		}  
		 
		int	typeid = (Integer)session.getAttribute("questionTypeId");  
		  
		
	 
	    if (question.getPageType().equals("calc"))
	    {
	    	IShapeCalcQusition q = (IShapeCalcQusition)question;
	    	q.setAreaAnswer(answer.areaAnswer);
	    	q.setPerimeterAnswer(answer.perimeterAnswer); 
	    
	    }else
	    {
	    	IShapeJudgeQusition q = (IShapeJudgeQusition)question;
	    	q.setJudge(answer.judegAnswer);	    	 
	    }  
	    Question myquestion = new Question();
	    myquestion.setUserId(u.getUserId());
	    myquestion.setTypeId(typeid);
	    myquestion.setContent(question.getQusitionString());
	    myquestion.setAnswer(question.getAnsewerString());
	    myquestion.setAnswerInfo(question.getAnswerInfo());
	    myquestion.setRightAnswer(question.getRightAnswerString());
	     
	    questionService.insertQuestion(myquestion);   
	    
	    code.setCode(1);
		code.setMsg(question.getAnswerInfo());  
		code.setRightAnswer(question.getRightAnswerString());  
	    return code;
	}  
}